package com.h3c.ptability.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eos.common.util.IdWorker;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.h3c.ptability.cache.UserCache;
import com.h3c.ptability.entity.PtMenu;
import com.h3c.ptability.mapper.PtMenuMapper;
import com.h3c.ptability.service.CommService;
import com.h3c.ptability.service.IPtMenuService;
import com.h3c.ptability.dto.PtMenuDTO;
import com.h3c.ptability.utils.InitExampleOrderByUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Copyright (C), 2020-2020, h3c
 * FileName: PtMenuServiceImpl
 *
 * @Author: l22886
 * @Date: 2022-03-14
 * Description: pt_menu serviceImpl
 * Version: 1.0
 */
@Slf4j
@Service
public class PtMenuServiceImpl extends ServiceImpl<PtMenuMapper, PtMenu> implements IPtMenuService {

    @Resource
    private PtMenuMapper ptMenuMapper;

    @Resource
    private CommService commService;

    

    /**
     * @title: selectFuzzyByDto
     * @description: 模糊查询操作
     * @params: [ dto ]
     * @return: java.util.List
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public List<PtMenuDTO> selectFuzzyByDto(PtMenuDTO dto) {
        //List<PtMenu> list = ptMenuMapper.selectByExample(initFuzzyExample(dto));
        List<PtMenuDTO> list = ptMenuMapper.queryListByFuzzy(dto);
        for (PtMenuDTO menuDTO : list) {
            menuDTO.setHasChildren(menuDTO.getCount() > 0);
        }
        return list;
    }

    /**
     * @title: selectByDto
     * @description: 精确查询操作
     * @params: [ dto ]
     * @return: java.util.List
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public List<PtMenuDTO> selectByDto(PtMenuDTO dto) {
        //List<PtMenu> list = ptMenuMapper.selectByExample(initExample(dto));
        List<PtMenuDTO> list = ptMenuMapper.queryList(dto);
        return list;
    }

    /**
     * @title: insertDto
     * @description: 插入操作
     * @params: [ dto ]
     * @return: Integer
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public Integer insertDto(PtMenuDTO dto) {
        //if (null == dto.getId() || "".equals(dto.getId())) {
        //    String pkId = idWorker.nextId() + "";
        //    dto.setId(pkId);
        //}
        dto.setItemId(IdWorker.nextId() + "");
        dto.setCreatorId(UserCache.getLoginUserId());
        dto.setCreateTime(new Date());
        return ptMenuMapper.insertPtMenu(JSONObject.parseObject(JSON.toJSONString(dto), PtMenu.class));
    }

    /**
     * @title: updateDto
     * @description: 修改操作
     * @params: [ dto ]
     * @return: Integer
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public Integer updateDto(PtMenuDTO dto) {
        return ptMenuMapper.updatePtMenu(dto);
    }

    /**
     * @title: deleteDto
     * @description: 删除操作
     * @params: [ dto ]
     * @return: Integer
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    @Override
    public Integer deleteDto(PtMenuDTO dto) {
        LinkedList<String> parentId = new LinkedList<>();
        parentId.add(dto.getItemId());
        delChildrenMenu(parentId);
        return 1;
    }

    public void delChildrenMenu(List<String> parentIds){
        remove(new QueryWrapper<PtMenu>().in("item_id",parentIds));
        List<PtMenu> childrenMenu = list(new QueryWrapper<PtMenu>().in("parent_item_id", parentIds));
        if (childrenMenu.isEmpty()) {
            return;
        }
        List<String> childrenIds = childrenMenu.stream().map(PtMenu::getItemId).collect(Collectors.toList());
        delChildrenMenu(childrenIds);
    }


    /**
     * @title: initFuzzyExample
     * @description: 初始化 模糊查询 example
     * @params: [ dto ]
     * @return: tk.mybatis.mapper.entity.Example
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    private Example initFuzzyExample(PtMenuDTO dto) {
        Example example = InitExampleOrderByUtils.initExampleOrderBy(PtMenu.class, dto.getOrderBy());
        Example.Criteria criteriaName = example.createCriteria();
        if (null != dto) {
            if (null != dto.getItemId() && !"".equals(dto.getItemId())) {
                criteriaName.andLike("ItemId", "%" + dto.getItemId() + "%");
            }
            if (null != dto.getAppId() && !"".equals(dto.getAppId())) {
                criteriaName.andLike("AppId", "%" + dto.getAppId() + "%");
            }
            if (null != dto.getItemType() && !"".equals(dto.getItemType())) {
                criteriaName.andLike("ItemType", "%" + dto.getItemType() + "%");
            }
            if (null != dto.getItemName() && !"".equals(dto.getItemName())) {
                criteriaName.andLike("ItemName", "%" + dto.getItemName() + "%");
            }
            if (null != dto.getItemNameEn() && !"".equals(dto.getItemNameEn())) {
                criteriaName.andLike("ItemNameEn", "%" + dto.getItemNameEn() + "%");
            }
            if (null != dto.getItemPath() && !"".equals(dto.getItemPath())) {
                criteriaName.andLike("ItemPath", "%" + dto.getItemPath() + "%");
            }
            if (null != dto.getParentItemId() && !"".equals(dto.getParentItemId())) {
                criteriaName.andLike("ParentItemId", "%" + dto.getParentItemId() + "%");
            }
            if (null != dto.getAuthFlag() && !"".equals(dto.getAuthFlag())) {
                criteriaName.andLike("AuthFlag", "%" + dto.getAuthFlag() + "%");
            }
            if (null != dto.getUseFlag() && !"".equals(dto.getUseFlag())) {
                criteriaName.andLike("UseFlag", "%" + dto.getUseFlag() + "%");
            }
            if (null != dto.getMenuIcon() && !"".equals(dto.getMenuIcon())) {
                criteriaName.andLike("MenuIcon", "%" + dto.getMenuIcon() + "%");
            }
            if (null != dto.getMenuTarget() && !"".equals(dto.getMenuTarget())) {
                criteriaName.andLike("MenuTarget", "%" + dto.getMenuTarget() + "%");
            }
            if (null != dto.getSortId() && !"".equals(dto.getSortId())) {
                criteriaName.andLike("SortId", "%" + dto.getSortId() + "%");
            }
            if (null != dto.getItemRemark() && !"".equals(dto.getItemRemark())) {
                criteriaName.andLike("ItemRemark", "%" + dto.getItemRemark() + "%");
            }
            if (null != dto.getCreateTime() && !"".equals(dto.getCreateTime())) {
                criteriaName.andLike("CreateTime", "%" + dto.getCreateTime() + "%");
            }
            if (null != dto.getCreatorId() && !"".equals(dto.getCreatorId())) {
                criteriaName.andLike("CreatorId", "%" + dto.getCreatorId() + "%");
            }
            if (null != dto.getUpdateTime() && !"".equals(dto.getUpdateTime())) {
                criteriaName.andLike("UpdateTime", "%" + dto.getUpdateTime() + "%");
            }
            if (null != dto.getUpdatorId() && !"".equals(dto.getUpdatorId())) {
                criteriaName.andLike("UpdatorId", "%" + dto.getUpdatorId() + "%");
            }
        }
        return example;
    }

    /**
     * @title: initExample
     * @description: 初始化 精确查询 example
     * @params: [ dto ]
     * @return: tk.mybatis.mapper.entity.Example
     * @createTime: 2022-03-14
     * @version: 1.0
     * @author: l22886
     */
    private Example initExample(PtMenuDTO dto) {
        Example example = InitExampleOrderByUtils.initExampleOrderBy(PtMenu.class, dto.getOrderBy());
        Example.Criteria criteriaName = example.createCriteria();
        if (null != dto) {
            if (null != dto.getItemId() && !"".equals(dto.getItemId())) {
                criteriaName.andEqualTo("ItemId", dto.getItemId());
            }
            if (null != dto.getAppId() && !"".equals(dto.getAppId())) {
                criteriaName.andEqualTo("AppId", dto.getAppId());
            }
            if (null != dto.getItemType() && !"".equals(dto.getItemType())) {
                criteriaName.andEqualTo("ItemType", dto.getItemType());
            }
            if (null != dto.getItemName() && !"".equals(dto.getItemName())) {
                criteriaName.andEqualTo("ItemName", dto.getItemName());
            }
            if (null != dto.getItemNameEn() && !"".equals(dto.getItemNameEn())) {
                criteriaName.andEqualTo("ItemNameEn", dto.getItemNameEn());
            }
            if (null != dto.getItemPath() && !"".equals(dto.getItemPath())) {
                criteriaName.andEqualTo("ItemPath", dto.getItemPath());
            }
            if (null != dto.getParentItemId() && !"".equals(dto.getParentItemId())) {
                criteriaName.andEqualTo("ParentItemId", dto.getParentItemId());
            }
            if (null != dto.getAuthFlag() && !"".equals(dto.getAuthFlag())) {
                criteriaName.andEqualTo("AuthFlag", dto.getAuthFlag());
            }
            if (null != dto.getUseFlag() && !"".equals(dto.getUseFlag())) {
                criteriaName.andEqualTo("UseFlag", dto.getUseFlag());
            }
            if (null != dto.getMenuIcon() && !"".equals(dto.getMenuIcon())) {
                criteriaName.andEqualTo("MenuIcon", dto.getMenuIcon());
            }
            if (null != dto.getMenuTarget() && !"".equals(dto.getMenuTarget())) {
                criteriaName.andEqualTo("MenuTarget", dto.getMenuTarget());
            }
            if (null != dto.getSortId() && !"".equals(dto.getSortId())) {
                criteriaName.andEqualTo("SortId", dto.getSortId());
            }
            if (null != dto.getItemRemark() && !"".equals(dto.getItemRemark())) {
                criteriaName.andEqualTo("ItemRemark", dto.getItemRemark());
            }
            if (null != dto.getCreateTime() && !"".equals(dto.getCreateTime())) {
                criteriaName.andEqualTo("CreateTime", dto.getCreateTime());
            }
            if (null != dto.getCreatorId() && !"".equals(dto.getCreatorId())) {
                criteriaName.andEqualTo("CreatorId", dto.getCreatorId());
            }
            if (null != dto.getUpdateTime() && !"".equals(dto.getUpdateTime())) {
                criteriaName.andEqualTo("UpdateTime", dto.getUpdateTime());
            }
            if (null != dto.getUpdatorId() && !"".equals(dto.getUpdatorId())) {
                criteriaName.andEqualTo("UpdatorId", dto.getUpdatorId());
            }
        }
        return example;
    }


}
